<?php

namespace App\Http\Resources\Estate\ElectricMeterResource;

use App\Models\ElectricCommunity;
use Illuminate\Http\Resources\Json\ResourceCollection;

class ElectricMeterCollection extends ResourceCollection
{
    protected $dates = ['create_time', 'update_time'];

    /**
     * Transform the resource collection into an array.
     *
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {
        $community_arr = ElectricCommunity::selectRaw('id value,name label')->where(['status' => 1])->get();
        return [

            'data' => $this->collection->map(function ($item) {
                return [
                    'id' => $item->id,
                    'name' => $item->name,
                    'code' => $item->code,
                    'tableAddress' => $item->tableAddress,
                    'electricity' => 0,
                    'community' => $item->community,
                    'community_name' => $item->one_community->name ?? "未绑定小区",
                    'collector' => $item->one_collector->name ?? "未绑定采集器",
                    'mult' => $item->mult ?? 1,
                    'brand' => $item->brand,
                    'charge' => $item->one_plan->plan_title ?? "未绑定缴费方案",
                    'alarm' => $item->one_alarm->plan_title ?? "未绑定报警方案",
                    'create_time' => $item->create_time,
                    'update_time' => $item->update_time,
                    'status' => $item->status,
                ];


            }),

            'total' => $this->total(), // 数据总数
            'per_page' => $this->perPage(), // 每页数量
            'current_page' => $this->currentPage(), // 当前页码
            'community_arr' => $community_arr,
        ];
    }
}
